package com.android.kmlmrg.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;

public class MethodeFile {
	/**LOG**/
	private static final  String LOG_TAG = MethodeFile.class.getSimpleName();


	
	public static String  getStringByteFileFromRes (Activity pActivity, int pIdRessource) {
		try {
			InputStream in = pActivity.getResources().openRawResource(pIdRessource);
			if (in != null) {
				InputStreamReader tmp = new InputStreamReader(in);
				BufferedReader reader = new BufferedReader(tmp);
				String str;
				StringBuilder buf = new StringBuilder();
				while ((str = reader.readLine()) != null) {
					buf.append(str);
				}

				in.close();
				return buf.toString();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	public static String getStringByteJpegFromRes  (Application pApplication, int pIdRessource) {
		Log.i(LOG_TAG, "START - getStringByteJpegFromRes  (Activity pActivity, int pIdRessource)");
		Bitmap bitmap= BitmapFactory.decodeResource(pApplication.getResources() , pIdRessource);
		ByteArrayOutputStream stream = new ByteArrayOutputStream();  
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
		byte[] image = stream.toByteArray();
		String vResult = Base64.encodeToString(image, Base64.DEFAULT);
		//Log.i(LOG_TAG, "IMAGE : " + vResult );
		
		Log.i(LOG_TAG, "END - getStringByteJpegFromRes  ");
		return vResult;

	}
	
	public static String getStringByteJpegFromFile  ( String pPath) {
		Log.i(LOG_TAG, "START - getStringByteJpegFromRes  (Activity pActivity, int pIdRessource)");
		Bitmap bitmap=  BitmapFactory.decodeFile(pPath);
		ByteArrayOutputStream stream = new ByteArrayOutputStream();  
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
		byte[] image = stream.toByteArray();
		String vResult = Base64.encodeToString(image, Base64.DEFAULT);
		//Log.i(LOG_TAG, "IMAGE : " + vResult );
		
		Log.i(LOG_TAG, "END - getStringByteJpegFromRes  ");
		return vResult;

	}
	
	

}
